//
//  MyCustomCell.m
//  KCFurProject
//
//  Created by iOS程序员 on 15/11/3.
//  Copyright (c) 2015年 iOS程序员 All rights reserved.
//

#import "MyCustomCell.h"


@implementation MyCustomCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //布局界面
        UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 5*HEIGH, W, 95*HEIGH)];
        bgView.backgroundColor = [UIColor whiteColor];
        
        //cell的线图片
        self.xianImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xian"]];
        self.xianImage.frame = CGRectMake(0, 110*HEIGH, W, 7*HEIGH);
        [bgView addSubview:self.xianImage];
        
        
        //添加商品图片
        _goodsImgV = [[UIImageView alloc]initWithFrame:CGRectMake(45*WIDTH, 10*HEIGH, 80*WIDTH, 80*HEIGH)];
        _goodsImgV.backgroundColor = [UIColor greenColor];
        [bgView addSubview:_goodsImgV];
        
        //添加商品标题
        _goodsTitleLab = [[UILabel alloc]initWithFrame:CGRectMake(130*WIDTH, 5*HEIGH, 250*WIDTH, 30*HEIGH)];
        _goodsTitleLab.text = @"afadsfa fa";
        _goodsTitleLab.backgroundColor = [UIColor clearColor];
        [bgView addSubview:_goodsTitleLab];
        
        //商品类型
        self.leiXingLabel = [[UILabel alloc] initWithFrame:CGRectMake(130*WIDTH, 35*HEIGH, 50*WIDTH, 15*HEIGH)];
        self.leiXingLabel.text = @"类型:";
        self.leiXingLabel.font = [UIFont systemFontOfSize:12];
        [bgView addSubview:self.leiXingLabel];
        
        //商品颜色
        self.yanSeLabel = [[UILabel alloc] initWithFrame:CGRectMake(190*WIDTH, 35*HEIGH, 50*WIDTH, 15*HEIGH)];
        self.yanSeLabel.text = @"颜色";
        self.yanSeLabel.font = [UIFont systemFontOfSize:12];
        [bgView addSubview:self.yanSeLabel];
        
        
        //促销价
        _priceTitleLab = [[UILabel alloc]initWithFrame:CGRectMake(124*WIDTH, 55*HEIGH, 70*WIDTH, 30*HEIGH)];
        _priceTitleLab.text = @"  ￥:  ";
        _priceTitleLab.backgroundColor = [UIColor clearColor];
        [bgView addSubview:_priceTitleLab];
        
        //商品价格
        _priceLab = [[UILabel alloc]initWithFrame:CGRectMake(150*WIDTH, 55*HEIGH, 100*WIDTH, 30*HEIGH)];
        _priceLab.text = @"1990";
        _priceLab.textColor = [UIColor redColor];
        [bgView addSubview:_priceLab];
        
        //购买数量
        _goodsNumLab = [[UILabel alloc]initWithFrame:CGRectMake(130*WIDTH, 65*HEIGH, 90*WIDTH, 30*HEIGH)];
//        _goodsNumLab.text = @"购买数量：";
        [bgView addSubview:_goodsNumLab];
        
        //减按钮
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteBtn.frame = CGRectMake(220*WIDTH, 65*HEIGH, 30*WIDTH, 30*HEIGH);
        [_deleteBtn setImage:[UIImage imageNamed:@"jian"] forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _deleteBtn.tag = 11;
        [bgView addSubview:_deleteBtn];
        
        //购买商品的数量
        _numCountLab = [[UILabel alloc]initWithFrame:CGRectMake(250*WIDTH, 65*HEIGH, 50*WIDTH, 30*HEIGH)];
        _numCountLab.textAlignment = NSTextAlignmentCenter;
        [bgView addSubview:_numCountLab];
        
        //加按钮
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addBtn.frame = CGRectMake(300*WIDTH, 65*HEIGH, 30*WIDTH, 30*HEIGH);
        [_addBtn setImage:[UIImage imageNamed:@"jia"] forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _addBtn.tag = 12;
        [bgView addSubview:_addBtn];
        
        //是否选中图片
        _isSelectImg = [[UIImageView alloc]initWithFrame:CGRectMake(5*WIDTH, 30*HEIGH, 20*WIDTH, 20*HEIGH)];
        [bgView addSubview:_isSelectImg];
        
        [self addSubview:bgView];
    }
    return self;
}

/**
 *  给单元格赋值
 *
 *  @param goodsModel 里面存放各个控件需要的数值
 */
-(void)addTheValue:(GoodsInfoModel *)goodsModel
{
    _goodsImgV.image = [UIImage imageNamed:goodsModel.imageName];
    _goodsTitleLab.text = goodsModel.goodsTitle;
    _priceLab.text = goodsModel.goodsPrice;
    _numCountLab.text = [NSString stringWithFormat:@"%d",goodsModel.goodsNum];
    self.leiXingLabel.text = goodsModel.leiXing;
    self.yanSeLabel.text = goodsModel.yanSe;
    
    if (goodsModel.selectState)
    {
        _selectState = YES;
        _isSelectImg.image = [UIImage imageNamed:@"xuanzhong"];
    }else{
        _selectState = NO;
        _isSelectImg.image = [UIImage imageNamed:@"yuan"];
    }
    
}

/**
 *  点击减按钮实现数量的减少
 *
 *  @param sender 减按钮
 */
-(void)deleteBtnAction:(UIButton *)sender
{
    //判断是否选中，选中才能点击
    if (_selectState == YES)
    {
        //调用代理
        [self.delegate btnClick:self andFlag:(int)sender.tag];
    }

}
/**
 *  点击加按钮实现数量的增加
 *
 *  @param sender 加按钮
 */
-(void)addBtnAction:(UIButton *)sender
{
    //判断是否选中，选中才能点击
    if (_selectState == YES)
    {
        //调用代理
        [self.delegate btnClick:self andFlag:(int)sender.tag];
    }

}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
